/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserPackage;

import java.text.ParseException;
import DBConnection.*;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
/**
 *
 * @author Hiroroi
 */
public class ProductManager {
    ResultSet res = null;
    
    public Product[] viewBooks()
    {
        try
        {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM products WHERE productType='1'");
            res = pstmt.executeQuery();
            res.last();
            int length = res.getRow();
            Product[] productResult = new Product[length];
            res.first();
            int i = 0;
            do
            {
                productResult[i] = new Product();
                productResult[i].setBookTitle(res.getString(4));
                productResult[i].setBookAuthor(res.getString(5));
                productResult[i].setBookYear(res.getInt(6));
                productResult[i].setSynopsis(res.getString(18));
                productResult[i].setPrice(res.getString(19));
                i++;
            }while(res.next());
            conn.close();
            return productResult;          
        }
        catch(SQLException ex)
        {
            return null;
        }
    }
            public Product[] viewMags()
    {
        try
        {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM products WHERE productType='2'");
            res = pstmt.executeQuery();
            res.last();
            int length = res.getRow();
            Product[] productResult = new Product[length];
            res.first();
            int i = 0;
            do
            {
                productResult[i] = new Product();
                productResult[i].setMagTitle(res.getString(7));
                productResult[i].setMagDate(res.getString(8));
                productResult[i].setMagVolNo(res.getInt(9));
                productResult[i].setMagIssueNo(res.getInt(10));
                productResult[i].setSynopsis(res.getString(18));
                productResult[i].setPrice(res.getString(19));
                i++;
            }while(res.next());
            conn.close();
            return productResult;          
        }
        catch(SQLException ex)
        {
            return null;
        }
    }
    
        public Product[] viewCds()
    {
        try
        {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM products WHERE productType='3'");
            res = pstmt.executeQuery();
            res.last();
            int length = res.getRow();
            Product[] productResult = new Product[length];
            res.first();
            int i = 0;
            do
            {
                productResult[i] = new Product();
                productResult[i].setCdAlbum(res.getString(12));
                productResult[i].setCdArtist(res.getString(11));
                productResult[i].setCdDate(res.getString(13));
                productResult[i].setCdTrackNo(res.getInt(14));
                productResult[i].setSynopsis(res.getString(18));
                productResult[i].setPrice(res.getString(19));
                i++;
            }while(res.next());
            conn.close();
            return productResult;          
        }
        catch(SQLException ex)
        {
            return null;
        }
    }
    
        public Product[] viewDvds()
    {
        try
        {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM products WHERE productType='4'");
            res = pstmt.executeQuery();
            res.last();
            int length = res.getRow();
            Product[] productResult = new Product[length];
            res.first();
            int i = 0;
            do
            {
                productResult[i] = new Product();
                productResult[i].setDvdTitle(res.getString(15));
                productResult[i].setDvdDirector(res.getString(16));
                productResult[i].setDvdYear(res.getInt(17));
                productResult[i].setSynopsis(res.getString(18));
                productResult[i].setPrice(res.getString(19));
                i++;
            }while(res.next());
            conn.close();
            return productResult;          
        }
        catch(SQLException ex)
        {
            return null;
        }
    }
        
     public boolean addBook(Product newBook) throws ParseException
    {
        boolean result=false;
        
        try
        {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO products(productBookTitle, productBookAuthor, productBookYear, productSynopsis, productPrice, productType) VALUES(?,?,?,?,?,?)");
            int i =1;
            
            pstmt.setString(i++, newBook.getBookTitle());
            pstmt.setString(i++, newBook.getBookAuthor());
            pstmt.setInt(i++, newBook.getBookYear());
            pstmt.setString(i++, newBook.getSynopsis());
            pstmt.setString(i++, newBook.getPrice());
            pstmt.setInt(i++, 1);
            pstmt.executeUpdate();
            conn.close();
            result= true;
            
        }
        catch(SQLException ex)
        {
           ex.printStackTrace(); 
        }
        
        return result;
        
    }
     
         public boolean addCd(Product newCd) throws ParseException
    {
        boolean result=false;
        
        try
        {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO products(productCdArtist, productCdAlbum, productCdDate, productCdTrackNo, productSynopsis, productPrice, productType) VALUES(?,?,?,?,?,?,?)");
            int i =1;
            
            pstmt.setString(i++, newCd.getCdArtist());
            pstmt.setString(i++, newCd.getCdAlbum());
            pstmt.setString(i++, newCd.getCdDate());
            pstmt.setInt(i++, newCd.getCdTrackNo());
            pstmt.setString(i++, newCd.getSynopsis());
            pstmt.setString(i++, newCd.getPrice());
            pstmt.setInt(i++, 3);   
            
            pstmt.executeUpdate();
            conn.close();
            result= true;
            
        }
        catch(Exception ex)
        {
           ex.printStackTrace(); 
        }
        
        return result;
        
    }
         
    public boolean addDvd(Product newDvd) throws ParseException
    {
        boolean result=false;
        
        try
        {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO products(productDvdTitle, productDvdDirector, productDvdYear, productSynopsis, productPrice, productType) VALUES(?,?,?,?,?,?)");
            int i =1;
            
            pstmt.setString(i++, newDvd.getDvdTitle());
            pstmt.setString(i++, newDvd.getDvdDirector());
            pstmt.setInt(i++, newDvd.getDvdYear());
            pstmt.setString(i++, newDvd.getSynopsis());
            pstmt.setString(i++, newDvd.getPrice());
            pstmt.setInt(i++, 4);
            
            pstmt.executeUpdate();
            conn.close();
            result= true;
            
        }
        catch(Exception ex)
        {
           ex.printStackTrace(); 
        }
        
        return result;
        
    }

     
    public boolean addMag(Product newMag) throws ParseException
    {
        boolean result=false;
        
        try
        {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO products(productMagTitle, productMagDate, productMagVolNo, productMagIssueNo, productSynopsis, productPrice, productType) VALUES(?,?,?,?,?,?,?)");
            int i =1;
            
            pstmt.setString(i++, newMag.getMagTitle());
            pstmt.setString(i++, newMag.getMagDate());
            pstmt.setInt(i++, newMag.getMagVolNo());
            pstmt.setInt(i++, newMag.getMagIssueNo());
            pstmt.setString(i++, newMag.getSynopsis());
            pstmt.setString(i++, newMag.getPrice());
            pstmt.setInt(i++, 2);
            
            pstmt.executeUpdate();
            conn.close();
            result= true;
            
        }
        catch(Exception ex)
        {
           ex.printStackTrace(); 
        }
        
        return result;
        
    }
     
     
}
    
